import {BANNER, DJ_BANNER,HIGH_QUALITY} from '../api'

const defaultState ={
  banners:[],
  djBanners:[],
  topPlayList:[]
}

export default (state=defaultState,action)=>{
  switch(action.type){
    case 'BANNER':
      return{...state,banners:action.banners};
    case 'DJ_BANNER':
      return {...state,djBanners:action.djBanners};
    case 'HIGH_QUALITY':
      return{...state,topPlayList:action.topPlayList}
    default:
      return state
  }
}

//banner区
export const getRecommend =() =>(dispatch) =>{
  // fetch 使用起来和axios差不多 
  // 一般的ajax 基于XHR 对象实现异步请求
  // fetch 不基于XHR  独立的进行数据请求的新的方式 不用安装第三方模块
  fetch(BANNER).then((response)=>response.json(
    console.log(response)
  )).then(result=>{
 
    console.log(result)
    console.log(11111111111111111111111111111111111111111111)
    dispatch({
      type:'BANNER',
      banners:result.banners
    })
  })
}


//DJ banner区
export const getDJBanner=()=>(dispatch)=>{
  fetch(DJ_BANNER).then((response)=>response.json()).then(result=>{
    console.log(result)
    //result.data.pop();// 删除多余的数据
    dispatch({
      type:'DJ_BANNER',
      djBanners:result.data
    })
  })
}

//精品歌单推荐
export const getTopPlayList =() =>(dispatch)=>{
  fetch(HIGH_QUALITY).then(response=>response.json()).then(result=>{
    console.log(result.playlists)

    dispatch({
      type: 'HIGH_QUALITY',
      topPlayList:result.playlists

    })
  })
}